//
//  VideoShareView.m
//  ShowYork
//
//  Created by hxh on 2020/11/27.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);
#define CONTAINWIDTH 280

#import "VideoShareView.h"
@interface VideoShareView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,copy)doneBlock done;
@end


@implementation VideoShareView

- (instancetype)initWithCompleteBlock:(void (^)(XHShareViewPlatformType))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}
- (void)setUI{
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
    self.frame = CGRectMake(0, 0, maxW, maxH);
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = RGBA(0, 0, 0, 0.7);
    [self addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.width.mas_equalTo(CONTAINWIDTH);
        make.right.equalTo(self).offset(CONTAINWIDTH);
    }];
    
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"你想分享到...";
    self.titleLb.textColor = UIColor.whiteColor;
    self.titleLb.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containView);
        make.top.equalTo(self.containView).offset(85);
    }];
    
    CGFloat leftMargin = 55;
    
    CGFloat bottomMarign = 50;
    
    CGFloat topMargin = 32;
    
    CGFloat itemW = 36;
    
    
    CGFloat itemMargin = (CONTAINWIDTH - 55 - 50 - itemW * 3)/2;

    NSArray *titleArray = @[@"微信好友",@"朋友圈",@"QQ",@"QQ空间",@"微博"];
    NSArray *imageArray = @[@"common_share_wechatSession_tag",@"common_share_wechatTimeLine_tag",@"common_share_qq_tag",@"common_share_qqzone_tag",@"common_share_weibo_tag"];
    

    
    for (int index = 0; index < titleArray.count; index++) {
        NSInteger row = index%3;
        NSInteger colum = index/3;
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = index;
        [btn setImage:[UIImage imageNamed:imageArray[index]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageArray[index]] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(shareMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.containView addSubview:btn];
        
        UILabel *lb = [[UILabel alloc]init];
        lb.text = titleArray[index];
        lb.textColor = UIColor.whiteColor;
        lb.font = font(12);
        [self.containView addSubview:lb];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.containView).offset(leftMargin + row * (itemMargin + itemW));
            make.top.equalTo(self.titleLb.mas_bottom).offset(topMargin + colum * (itemMargin + bottomMarign));
            make.size.mas_equalTo(CGSizeMake(itemW, itemW));
        }];
                
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btn);
            make.top.equalTo(btn.mas_bottom).offset(8);
        }];
      
    }
    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

- (void)shareMethod:(UIButton *)button{
    if (self.done) {
        self.done(button.tag);
    }
    [self dismiss];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self layoutIfNeeded];
    
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
           make.right.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)dismiss{
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(CONTAINWIDTH);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}
@end
